package com.zhr.tea.view.adapter;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.zhr.tea.R;
import com.zhr.tea.entity.CarBean;
import com.zhr.tea.utils.IsLogin;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by 毛懿 on 2016/10/31.
 */

public class CarAdapter extends RecyclerView.Adapter {
    public static final int TYPE_HEAD = -1;
    public static final int TYPE_CONTENT = -2;

    private Context context;
    private List<CarBean.ContentBean> data;
    private ILogin iLogin;


    public CarAdapter(Context context, List<CarBean.ContentBean> data, ILogin iLogin) {
        this.context = context;
        this.data = data;
        this.iLogin = iLogin;
    }

    private void setOnLogin() {
        iLogin.Login();
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return TYPE_HEAD;
        } else {
            return TYPE_CONTENT;
        }

    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case TYPE_HEAD:
                return new HeadViewHolder(LayoutInflater.from(context).inflate(R.layout.item_car_head, parent, false));
            case TYPE_CONTENT:
                return new ItemViewHolder(LayoutInflater.from(context).inflate(R.layout.item_car_foot, parent, false));
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (position == 0) {
            HeadViewHolder holder1 = (HeadViewHolder) holder;
            if (IsLogin.isLogin) {
                holder1.carLoginBefore.setVisibility(View.GONE);
                holder1.carLoginAfter.setVisibility(View.VISIBLE);
            } else {
                holder1.carLoginBefore.setVisibility(View.VISIBLE);
                holder1.carLoginAfter.setVisibility(View.GONE);
            }
            holder1.carLogin.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    setOnLogin();
                }
            });
        } else {
            ItemViewHolder holder2 = (ItemViewHolder) holder;
            Glide.with(context).load(data.get(position - 1).getGoodsImgUrl()).placeholder(R.mipmap.abc_ic_menu_copy_mtrl_am_alpha).into(holder2.carFootIv);
            holder2.carFootTvTitle.setText(data.get(position - 1).getGoodsName());
            holder2.carFootTvPrice.setText("￥" + data.get(position - 1).getGoodsPrice());
            holder2.carFootTvCount.setText("已售" + data.get(position - 1).getSaleCount() + "份");
        }
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        GridLayoutManager manager = (GridLayoutManager) recyclerView.getLayoutManager();
        manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                if (getItemViewType(position) == TYPE_HEAD) {
                    return 2;
                } else {
                    return 1;
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return data.size() + 1;
    }

    public class HeadViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.car_login)
        ImageView carLogin;
        @BindView(R.id.car_login_before)
        LinearLayout carLoginBefore;
        @BindView(R.id.car_login_after)
        LinearLayout carLoginAfter;

        public HeadViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public class ItemViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.car_foot_iv)
        ImageView carFootIv;
        @BindView(R.id.car_foot_tv_title)
        TextView carFootTvTitle;
        @BindView(R.id.car_foot_tv_price)
        TextView carFootTvPrice;
        @BindView(R.id.car_foot_tv_count)
        TextView carFootTvCount;

        public ItemViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface ILogin {
        void Login();
    }
}
